import { createRouter, createWebHistory } from 'vue-router'
import Main from '@/layouts/index.vue'
import { useUser } from '@/stores/user'
/**
 * meta: {
 *  auth 是否校验登入
 * }
*/

const router = createRouter({
  history: createWebHistory('/'),
  routes: [
    {
      path: '/',
      name: 'Home',
      component: Main,
      meta: {
        title: '首页'
      },
      redirect: '/home',
      children: [
        {
          path: '/home',
          name: 'Home',
          component: () => import('@/views/home/index.vue')
        }
      ]
    },
    {
      path: '/personal-center',
      name: 'PersonalCenter',
      component: Main,
      meta: {
        title: '',
        auth: true
      },
      children: [
        {
          path: '/personal-center/center',
          name: 'Center',
          component: () => import('@/views/personal-center/index.vue')
        }
      ]
    },
    {
      path: '/login',
      name: 'Login',
      component: () => import('@/views/login/index.vue'),
      meta: {
        title: ''
      }
    },
    {
      path: '/attract',
      name: 'Attract',
      component: Main,
      meta: {
        title: ''
      },
      children: [
        {
          path: 'agreement',
          name: 'Agreement',
          meta: {
          },
          component: () => import('@/views/agreement/index.vue')
        },
        {
          path: 'settled',
          name: 'Settled',
          meta: {
          },
          component: () => import('@/views/settled/index.vue')
        }
      ]
    },
    {
      path: '/look',
      name: 'Look',
      component: Main,
      meta: {
        title: ''
      },
      children: [
        {
          path: 'information',
          name: 'Information',
          component: () => import('@/views/information/index.vue')
        },
        {
          path: 'details',
          name: 'LookDetails',
          component: () => import('@/views/information/details/index.vue')
        }
      ]
    },
    {
      path: '/universe',
      name: 'Universe',
      component: Main,
      meta: {
        title: ''
      },
      children: [
        {
          path: 'college',
          name: 'College',
          component: () => import('@/views/college/index.vue')
        },
        {
          path: 'details',
          name: 'UniverseDetails',
          component: () => import('@/views/college/details/index.vue')
        }
      ]
    },
    {
      path: '/:pathMatch(.*)*',
      meta: {
        title: '404',
        hideInMenu: true
      },
      component: () => import('@/components/no-found.vue')
    },
  ],
  scrollBehavior() {
    return { top: 0, left: 0 }
  }
})

router.beforeEach(async (to, from, next) => {
  const user_store = useUser()
  user_store.current_nav = user_store.nav_name_list.indexOf(to.name)

  if (!to.meta.auth) {
    next()
  } else {
    if (!user_store.is_login) {
      next({
        name: "Login",
      })
    } else {
      next()
    }
  }
})

export default router
